using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace demo
{
    /// <summary>
    /// 消息通知弹出窗体
    /// 在屏幕右下角显示消息通知，支持自动消失和用户交互
    /// </summary>
    public partial class MessageNotificationForm : Form
    {
        #region Win32 API
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        
        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private const uint SWP_NOSIZE = 0x0001;
        private const uint SWP_NOMOVE = 0x0002;
        private const uint SWP_SHOWWINDOW = 0x0040;
        #endregion

        #region 私有字段
        private Timer autoCloseTimer;
        private Timer slideTimer;
        private int targetY;
        private int startY;
        private bool isClosing = false;
        private const int SLIDE_SPEED = 10;
        private const int AUTO_CLOSE_DELAY = 180000; // 3分钟后自动关闭
        #endregion

        #region 控件
        private Label lblTitle;
        private Label lblMessage;
        private Label lblTime;
        private Button btnClose;
        private Button btnAction1;
        private Button btnAction2;
        private Panel pnlContent;
        
        // 患者信息列表显示控件
        private Label lblPatientNameHeader;
        private Label lblPatientName;
        private Label lblRegistrationStatusHeader;
        private Label lblRegistrationStatus;
        private Label lblPatientIdHeader;
        private Label lblPatientId;
        private Button btnViewDetails;
        private Label lblPatientLevel;
        private Label lblPatientIdLevel;
        private Panel panelPatientColor;
        private Button btnIgnore;
        #endregion

        #region 事件
        /// <summary>
        /// 通知被点击事件
        /// </summary>
#pragma warning disable CS0067 // 事件从不使用
        public event EventHandler<MessageNotificationEventArgs> NotificationClicked;
#pragma warning restore CS0067
        
        /// <summary>
        /// 通知被关闭事件
        /// </summary>
        public event EventHandler<MessageNotificationEventArgs> NotificationClosed;
        
        /// <summary>
        /// 自定义按钮被点击事件
        /// </summary>
        public event EventHandler<MessageActionEventArgs> ActionButtonClicked;
        #endregion

        #region 属性
        /// <summary>
        /// 消息内容
        /// </summary>
        public string MessageContent { get; private set; }
        
        /// <summary>
        /// 消息类型
        /// </summary>
        public NotificationMessageType MessageType { get; private set; }
        
        /// <summary>
        /// 消息时间
        /// </summary>
        public DateTime MessageTime { get; private set; }
        
        /// <summary>
        /// 患者数据
        /// </summary>
        public PatientData CurrentPatientData { get; private set; }

        /// <summary>
        /// 需要显示的病人颜色
        /// </summary>
        public string PatientColor { get; set; }
        #endregion

        #region 构造函数
        /// <summary>
        /// 初始化消息通知窗体
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">消息内容</param>
        /// <param name="messageType">消息类型</param>
        public MessageNotificationForm(string title, string message, NotificationMessageType messageType = NotificationMessageType.Info)
        {
            InitializeComponent();
            InitializeNotification(title, message, messageType);
        }
        
        /// <summary>
        /// 初始化患者数据通知窗体
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="patientData">患者数据</param>
        /// <param name="messageType">消息类型</param>
        public MessageNotificationForm(string title, PatientData patientData, NotificationMessageType messageType = NotificationMessageType.Info)
        {
            CurrentPatientData = patientData;
            InitializeComponent();
            InitializePatientDataNotification(title, patientData, messageType);
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 格式化患者数据消息
        /// </summary>
        /// <param name="patientData">患者数据</param>
        /// <returns>格式化后的消息</returns>
        private string FormatPatientDataMessage(PatientData patientData)
        {
            if (patientData == null)
            {
                return "患者数据为空";
            }
            
            return string.Format(
                "定时查询发现新的患者数据:\n\n" +
                "患者姓名: {0}\n" +
                "就诊ID: {1}\n" +
                "就诊日期: {2}\n" +
                "查询时间: {3}",
                patientData.PatientName ?? "未知",
                patientData.OutpVisitId ?? "未知",
                patientData.OutpDate ?? "未知",
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            );
        }
        
        private void InitializePatientDataNotification(string title, PatientData patientData, NotificationMessageType messageType)
        {
            MessageContent = "患者数据通知";
            MessageType = messageType;
            MessageTime = DateTime.Now;
            
            lblTitle.Text = title;
            lblTime.Text = MessageTime.ToString("HH:mm:ss");
            
            // 设置患者信息
            if (patientData != null)
            {
                string PatientColor = patientData.DoctorVsOutpVisitId.Split('&')[3];
                lblPatientName.Text = patientData.PatientName ?? "未知";
                lblRegistrationStatus.Text = patientData.RegistrationFeeStatus ?? "未知";
                lblPatientId.Text = patientData.PatientId ?? "未知";
                lblPatientLevel.Text = patientData.PatLevel ?? "未知";
                this.panelPatientColor.BackColor = Color.FromName(PatientColor);
            }
            
            // 根据消息类型设置颜色
            switch (messageType)
            {
                case NotificationMessageType.Info:
                    pnlContent.BackColor = Color.FromArgb(240, 248, 255);
                    lblTitle.ForeColor = Color.FromArgb(0, 122, 204);
                    break;
                case NotificationMessageType.Warning:
                    pnlContent.BackColor = Color.FromArgb(255, 248, 220);
                    lblTitle.ForeColor = Color.FromArgb(255, 140, 0);
                    break;
                case NotificationMessageType.Error:
                    pnlContent.BackColor = Color.FromArgb(255, 240, 240);
                    lblTitle.ForeColor = Color.FromArgb(220, 53, 69);
                    break;
                case NotificationMessageType.Success:
                    pnlContent.BackColor = Color.FromArgb(240, 255, 240);
                    lblTitle.ForeColor = Color.FromArgb(40, 167, 69);
                    break;
            }
            
            this.BackColor = pnlContent.BackColor;
            
            // 初始化定时器
            InitializeTimers();
            
            // 设置窗体位置
            SetNotificationPosition();
        }
        #endregion

        #region 初始化方法
        private void InitializeComponent()
        {
            this.pnlContent = new System.Windows.Forms.Panel();
            this.panelPatientColor = new System.Windows.Forms.Panel();
            this.lblPatientLevel = new System.Windows.Forms.Label();
            this.lblPatientIdLevel = new System.Windows.Forms.Label();
            this.lblPatientId = new System.Windows.Forms.Label();
            this.btnViewDetails = new System.Windows.Forms.Button();
            this.lblTitle = new System.Windows.Forms.Label();
            this.lblMessage = new System.Windows.Forms.Label();
            this.lblTime = new System.Windows.Forms.Label();
            this.btnClose = new System.Windows.Forms.Button();
            this.btnAction1 = new System.Windows.Forms.Button();
            this.btnAction2 = new System.Windows.Forms.Button();
            this.lblPatientNameHeader = new System.Windows.Forms.Label();
            this.lblPatientName = new System.Windows.Forms.Label();
            this.lblRegistrationStatusHeader = new System.Windows.Forms.Label();
            this.lblRegistrationStatus = new System.Windows.Forms.Label();
            this.lblPatientIdHeader = new System.Windows.Forms.Label();
            this.btnIgnore = new System.Windows.Forms.Button();
            this.pnlContent.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlContent
            // 
            this.pnlContent.Controls.Add(this.panelPatientColor);
            this.pnlContent.Controls.Add(this.lblPatientLevel);
            this.pnlContent.Controls.Add(this.lblPatientIdLevel);
            this.pnlContent.Controls.Add(this.lblPatientId);
            this.pnlContent.Controls.Add(this.btnViewDetails);
            this.pnlContent.Controls.Add(this.lblTitle);
            this.pnlContent.Controls.Add(this.lblMessage);
            this.pnlContent.Controls.Add(this.lblTime);
            this.pnlContent.Controls.Add(this.btnClose);
            this.pnlContent.Controls.Add(this.btnAction1);
            this.pnlContent.Controls.Add(this.btnAction2);
            this.pnlContent.Controls.Add(this.lblPatientNameHeader);
            this.pnlContent.Controls.Add(this.lblPatientName);
            this.pnlContent.Controls.Add(this.lblRegistrationStatusHeader);
            this.pnlContent.Controls.Add(this.lblRegistrationStatus);
            this.pnlContent.Controls.Add(this.lblPatientIdHeader);
            this.pnlContent.Controls.Add(this.btnIgnore);
            this.pnlContent.Location = new System.Drawing.Point(0, 0);
            this.pnlContent.Name = "pnlContent";
            this.pnlContent.Size = new System.Drawing.Size(400, 200);
            this.pnlContent.TabIndex = 0;
            // 
            // panelPatientColor
            // 
            this.panelPatientColor.BackColor = System.Drawing.Color.MidnightBlue;
            this.panelPatientColor.Location = new System.Drawing.Point(0, 146);
            this.panelPatientColor.Name = "panelPatientColor";
            this.panelPatientColor.Size = new System.Drawing.Size(400, 92);
            this.panelPatientColor.TabIndex = 16;
            // 
            // lblPatientLevel
            // 
            this.lblPatientLevel.Font = new System.Drawing.Font("微软雅黑", 11F);
            this.lblPatientLevel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(100)))), ((int)(((byte)(180)))));
            this.lblPatientLevel.Location = new System.Drawing.Point(261, 49);
            this.lblPatientLevel.Name = "lblPatientLevel";
            this.lblPatientLevel.Size = new System.Drawing.Size(100, 25);
            this.lblPatientLevel.TabIndex = 15;
            this.lblPatientLevel.Text = "未知";
            // 
            // lblPatientIdLevel
            // 
            this.lblPatientIdLevel.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.lblPatientIdLevel.Location = new System.Drawing.Point(206, 51);
            this.lblPatientIdLevel.Name = "lblPatientIdLevel";
            this.lblPatientIdLevel.Size = new System.Drawing.Size(49, 25);
            this.lblPatientIdLevel.TabIndex = 14;
            this.lblPatientIdLevel.Text = "类型:";
            // 
            // lblPatientId
            // 
            this.lblPatientId.Font = new System.Drawing.Font("微软雅黑", 11F);
            this.lblPatientId.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(100)))), ((int)(((byte)(180)))));
            this.lblPatientId.Location = new System.Drawing.Point(89, 110);
            this.lblPatientId.Name = "lblPatientId";
            this.lblPatientId.Size = new System.Drawing.Size(111, 25);
            this.lblPatientId.TabIndex = 11;
            this.lblPatientId.Text = "未知";
            // 
            // btnViewDetails
            // 
            this.btnViewDetails.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(122)))), ((int)(((byte)(204)))));
            this.btnViewDetails.FlatAppearance.BorderSize = 0;
            this.btnViewDetails.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnViewDetails.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.btnViewDetails.ForeColor = System.Drawing.Color.White;
            this.btnViewDetails.Location = new System.Drawing.Point(204, 105);
            this.btnViewDetails.Name = "btnViewDetails";
            this.btnViewDetails.Size = new System.Drawing.Size(80, 35);
            this.btnViewDetails.TabIndex = 12;
            this.btnViewDetails.Text = "查阅";
            this.btnViewDetails.UseVisualStyleBackColor = false;
            this.btnViewDetails.Click += new System.EventHandler(this.BtnViewDetails_Click);
            // 
            // lblTitle
            // 
            this.lblTitle.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold);
            this.lblTitle.Location = new System.Drawing.Point(15, 15);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new System.Drawing.Size(320, 25);
            this.lblTitle.TabIndex = 0;
            // 
            // lblMessage
            // 
            this.lblMessage.Font = new System.Drawing.Font("微软雅黑", 9F);
            this.lblMessage.Location = new System.Drawing.Point(45, 145);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(330, 60);
            this.lblMessage.TabIndex = 1;
            this.lblMessage.Visible = false;
            // 
            // lblTime
            // 
            this.lblTime.Font = new System.Drawing.Font("微软雅黑", 8F);
            this.lblTime.ForeColor = System.Drawing.Color.Gray;
            this.lblTime.Location = new System.Drawing.Point(15, 145);
            this.lblTime.Name = "lblTime";
            this.lblTime.Size = new System.Drawing.Size(100, 20);
            this.lblTime.TabIndex = 2;
            // 
            // btnClose
            // 
            this.btnClose.FlatAppearance.BorderSize = 0;
            this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnClose.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold);
            this.btnClose.ForeColor = System.Drawing.Color.Gray;
            this.btnClose.Location = new System.Drawing.Point(365, 10);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(25, 25);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "×";
            this.btnClose.Click += new System.EventHandler(this.BtnClose_Click);
            // 
            // btnAction1
            // 
            this.btnAction1.FlatAppearance.BorderSize = 0;
            this.btnAction1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnAction1.Font = new System.Drawing.Font("微软雅黑", 8F);
            this.btnAction1.Location = new System.Drawing.Point(180, 165);
            this.btnAction1.Name = "btnAction1";
            this.btnAction1.Size = new System.Drawing.Size(75, 25);
            this.btnAction1.TabIndex = 4;
            this.btnAction1.Visible = false;
            this.btnAction1.Click += new System.EventHandler(this.BtnAction1_Click);
            // 
            // btnAction2
            // 
            this.btnAction2.FlatAppearance.BorderSize = 0;
            this.btnAction2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnAction2.Font = new System.Drawing.Font("微软雅黑", 8F);
            this.btnAction2.Location = new System.Drawing.Point(265, 165);
            this.btnAction2.Name = "btnAction2";
            this.btnAction2.Size = new System.Drawing.Size(75, 25);
            this.btnAction2.TabIndex = 5;
            this.btnAction2.Visible = false;
            this.btnAction2.Click += new System.EventHandler(this.BtnAction2_Click);
            // 
            // lblPatientNameHeader
            // 
            this.lblPatientNameHeader.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.lblPatientNameHeader.Location = new System.Drawing.Point(15, 50);
            this.lblPatientNameHeader.Name = "lblPatientNameHeader";
            this.lblPatientNameHeader.Size = new System.Drawing.Size(80, 25);
            this.lblPatientNameHeader.TabIndex = 6;
            this.lblPatientNameHeader.Text = "患者姓名:";
            // 
            // lblPatientName
            // 
            this.lblPatientName.Font = new System.Drawing.Font("微软雅黑", 11F);
            this.lblPatientName.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(100)))), ((int)(((byte)(180)))));
            this.lblPatientName.Location = new System.Drawing.Point(100, 50);
            this.lblPatientName.Name = "lblPatientName";
            this.lblPatientName.Size = new System.Drawing.Size(100, 25);
            this.lblPatientName.TabIndex = 7;
            this.lblPatientName.Text = "未知";
            // 
            // lblRegistrationStatusHeader
            // 
            this.lblRegistrationStatusHeader.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.lblRegistrationStatusHeader.Location = new System.Drawing.Point(15, 80);
            this.lblRegistrationStatusHeader.Name = "lblRegistrationStatusHeader";
            this.lblRegistrationStatusHeader.Size = new System.Drawing.Size(80, 25);
            this.lblRegistrationStatusHeader.TabIndex = 8;
            this.lblRegistrationStatusHeader.Text = "缴费状态:";
            // 
            // lblRegistrationStatus
            // 
            this.lblRegistrationStatus.Font = new System.Drawing.Font("微软雅黑", 11F);
            this.lblRegistrationStatus.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(100)))), ((int)(((byte)(180)))));
            this.lblRegistrationStatus.Location = new System.Drawing.Point(100, 80);
            this.lblRegistrationStatus.Name = "lblRegistrationStatus";
            this.lblRegistrationStatus.Size = new System.Drawing.Size(100, 25);
            this.lblRegistrationStatus.TabIndex = 9;
            this.lblRegistrationStatus.Text = "未知";
            // 
            // lblPatientIdHeader
            // 
            this.lblPatientIdHeader.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.lblPatientIdHeader.Location = new System.Drawing.Point(15, 110);
            this.lblPatientIdHeader.Name = "lblPatientIdHeader";
            this.lblPatientIdHeader.Size = new System.Drawing.Size(80, 25);
            this.lblPatientIdHeader.TabIndex = 10;
            this.lblPatientIdHeader.Text = "患者ID:";
            // 
            // btnIgnore
            // 
            this.btnIgnore.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
            this.btnIgnore.FlatAppearance.BorderSize = 0;
            this.btnIgnore.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnIgnore.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Bold);
            this.btnIgnore.ForeColor = System.Drawing.Color.White;
            this.btnIgnore.Location = new System.Drawing.Point(295, 105);
            this.btnIgnore.Name = "btnIgnore";
            this.btnIgnore.Size = new System.Drawing.Size(80, 35);
            this.btnIgnore.TabIndex = 13;
            this.btnIgnore.Text = "忽略";
            this.btnIgnore.UseVisualStyleBackColor = false;
            this.btnIgnore.Click += new System.EventHandler(this.BtnIgnore_Click);
            // 
            // MessageNotificationForm
            // 
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(400, 200);
            this.Controls.Add(this.pnlContent);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "MessageNotificationForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.TopMost = true;
            this.pnlContent.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        
        private void InitializeNotification(string title, string message, NotificationMessageType messageType)
        {
            MessageContent = message;
            MessageType = messageType;
            MessageTime = DateTime.Now;
            
            lblTitle.Text = title;
            lblMessage.Text = message;
            lblTime.Text = MessageTime.ToString("HH:mm:ss");
            
            // 根据消息类型设置颜色
            switch (messageType)
            {
                case NotificationMessageType.Info:
                    pnlContent.BackColor = Color.FromArgb(240, 248, 255);
                    lblTitle.ForeColor = Color.FromArgb(0, 122, 204);
                    break;
                case NotificationMessageType.Warning:
                    pnlContent.BackColor = Color.FromArgb(255, 248, 220);
                    lblTitle.ForeColor = Color.FromArgb(255, 140, 0);
                    break;
                case NotificationMessageType.Error:
                    pnlContent.BackColor = Color.FromArgb(255, 240, 240);
                    lblTitle.ForeColor = Color.FromArgb(220, 53, 69);
                    break;
                case NotificationMessageType.Success:
                    pnlContent.BackColor = Color.FromArgb(240, 255, 240);
                    lblTitle.ForeColor = Color.FromArgb(40, 167, 69);
                    break;
            }
            
            this.BackColor = pnlContent.BackColor;
            
            // 初始化定时器
            InitializeTimers();
            
            // 设置窗体位置
            SetNotificationPosition();
        }
        
        private void InitializeTimers()
        {
            // 自动关闭定时器
            autoCloseTimer = new Timer()
            {
                Interval = AUTO_CLOSE_DELAY,
                Enabled = false
            };
            autoCloseTimer.Tick += AutoCloseTimer_Tick;
            
            // 滑动动画定时器
            slideTimer = new Timer()
            {
                Interval = 20,
                Enabled = false
            };
            slideTimer.Tick += SlideTimer_Tick;
        }
        
        private void SetNotificationPosition()
        {
            // 获取工作区域
            Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
            
            // 计算目标位置（右下角）
            targetY = workingArea.Bottom - this.Height - 10;
            startY = workingArea.Bottom;
            
            // 设置初始位置（屏幕外）
            this.Location = new Point(workingArea.Right - this.Width - 10, startY);
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 显示通知
        /// </summary>
        public void ShowNotification()
        {
            // 设置窗体为最顶层
            SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
            
            this.Show();
            
            // 开始滑入动画
            slideTimer.Start();
        }
        
        /// <summary>
        /// 设置自定义按钮
        /// </summary>
        /// <param name="button1Text">按钮1文本</param>
        /// <param name="button2Text">按钮2文本</param>
        public void SetActionButtons(string button1Text = null, string button2Text = null)
        {
            if (!string.IsNullOrEmpty(button1Text))
            {
                btnAction1.Text = button1Text;
                btnAction1.Visible = true;
            }
            
            if (!string.IsNullOrEmpty(button2Text))
            {
                btnAction2.Text = button2Text;
                btnAction2.Visible = true;
            }
        }
        
        /// <summary>
        /// 关闭通知
        /// </summary>
        public void CloseNotification()
        {
            if (isClosing) return;
            
            isClosing = true;
            autoCloseTimer?.Stop();
            
            // 触发关闭事件
            NotificationClosed?.Invoke(this, new MessageNotificationEventArgs(MessageContent, MessageType, MessageTime));
            
            // 开始滑出动画
            startY = this.Location.Y;
            targetY = Screen.PrimaryScreen.WorkingArea.Bottom;
            slideTimer.Start();
        }
        #endregion

        #region 事件处理
        private void MessageNotificationForm_Paint(object sender, PaintEventArgs e)
        {
            // 绘制边框
            using (Pen pen = new Pen(Color.FromArgb(200, 200, 200), 1))
            {
                e.Graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
        
        private void MessageNotificationForm_MouseEnter(object sender, EventArgs e)
        {
            // 鼠标进入时停止自动关闭
            autoCloseTimer?.Stop();
        }
        
        private void MessageNotificationForm_MouseLeave(object sender, EventArgs e)
        {
            // 鼠标离开时重新开始自动关闭倒计时
            if (!isClosing)
            {
                autoCloseTimer?.Start();
            }
        }
        
        private void BtnClose_Click(object sender, EventArgs e)
        {
            CloseNotification();
        }
        
        private void BtnAction1_Click(object sender, EventArgs e)
        {
            ActionButtonClicked?.Invoke(this, new MessageActionEventArgs(MessageContent, MessageType, MessageTime, "Action1", btnAction1.Text));
        }
        
        private void BtnAction2_Click(object sender, EventArgs e)
        {
            ActionButtonClicked?.Invoke(this, new MessageActionEventArgs(MessageContent, MessageType, MessageTime, "Action2", btnAction2.Text));
        }
        
        private void BtnViewDetails_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrentPatientData != null && !string.IsNullOrEmpty(CurrentPatientData.QrCode))
                {
                    // 查找Form1实例
                    Form1 mainForm = null;
                    foreach (Form form in Application.OpenForms)
                    {
                        if (form is Form1)
                        {
                            mainForm = form as Form1;
                            break;
                        }
                    }
                    
                    if (mainForm != null)
                    {
                        // 设置QR_CODE到textBoxcx
                        mainForm.SetTextBoxcxValue(CurrentPatientData.QrCode);
                        
                        // 触发button2的Click事件
                        mainForm.TriggerButton2Click();

                        mainForm.SetBtLisExamStatus(CurrentPatientData.LisCount, CurrentPatientData.ExamCount, CurrentPatientData.DoctorVsOutpVisitId.Split('&')[4]);
                        // 关闭当前通知窗口
                        CloseNotification();
                    }
                    else
                    {
                        MessageBox.Show("无法找到主窗体", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("患者数据或二维码信息不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查阅操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void BtnIgnore_Click(object sender, EventArgs e)
        {
            // 直接关闭当前通知窗口，不执行任何其他操作
            CloseNotification();
        }
        
        private void AutoCloseTimer_Tick(object sender, EventArgs e)
        {
            CloseNotification();
        }
        
        private void SlideTimer_Tick(object sender, EventArgs e)
        {
            if (!isClosing)
            {
                // 滑入动画
                if (this.Location.Y > targetY)
                {
                    int newY = Math.Max(targetY, this.Location.Y - SLIDE_SPEED);
                    this.Location = new Point(this.Location.X, newY);
                    
                    if (newY == targetY)
                    {
                        slideTimer.Stop();
                        autoCloseTimer.Start(); // 开始自动关闭倒计时
                    }
                }
            }
            else
            {
                // 滑出动画
                if (this.Location.Y < targetY)
                {
                    int newY = Math.Min(targetY, this.Location.Y + SLIDE_SPEED);
                    this.Location = new Point(this.Location.X, newY);
                    
                    if (newY == targetY)
                    {
                        slideTimer.Stop();
                        this.Hide();
                        this.Dispose();
                    }
                }
            }
        }
        
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            autoCloseTimer?.Stop();
            slideTimer?.Stop();
            autoCloseTimer?.Dispose();
            slideTimer?.Dispose();
            base.OnFormClosing(e);
        }
        #endregion
    }

    #region 枚举和事件参数
    /// <summary>
    /// 消息类型枚举
    /// </summary>
    public enum NotificationMessageType
    {
        Info,
        Warning,
        Error,
        Success
    }
    
    /// <summary>
    /// 消息通知事件参数
    /// </summary>
    public class MessageNotificationEventArgs : EventArgs
    {
        public string Message { get; }
        public NotificationMessageType MessageType { get; }
        public DateTime MessageTime { get; }
        
        public MessageNotificationEventArgs(string message, NotificationMessageType messageType, DateTime messageTime)
        {
            Message = message;
            MessageType = messageType;
            MessageTime = messageTime;
        }
    }
    
    /// <summary>
    /// 消息操作事件参数
    /// </summary>
    public class MessageActionEventArgs : EventArgs
    {
        public string Message { get; }
        public NotificationMessageType MessageType { get; }
        public DateTime MessageTime { get; }
        public string ActionId { get; }
        public string ActionText { get; }
        
        public MessageActionEventArgs(string message, NotificationMessageType messageType, DateTime messageTime, string actionId, string actionText)
        {
            Message = message;
            MessageType = messageType;
            MessageTime = messageTime;
            ActionId = actionId;
            ActionText = actionText;
        }
    }
    #endregion
}